package lev01_beginnerClass.class03_dataStructurePreSum;


/**
 * build prefix sum arr
 */
public class L01_preSum {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        PreSum preSum = new PreSum(arr);
        System.out.println(preSum.getRangeSum(1,3));
    }

    public static class PreSum{
        int[] presum;

        public PreSum(int[] arr){
            int len = arr.length;
            presum = new int[len];
            presum[0] = arr[0];
            for(int i = 1;i < len;i++){
                presum[i] = presum[i-1]+arr[i];
            }
        }

        public int getRangeSum(int l,int r){
            return l == 0 ? presum[r]:presum[r] - presum[l-1];
        }

    }


}



